<!--
 * @Description: 
 * @Author: 胡涛
 * @Date: 2023-09-11 21:21:30
 * @LastEditors: 胡涛
 * @LastEditTime: 2023-11-12 14:24:48
-->
<template>
  <div class="page-wrapper">
    <div class="toolbar">
      <div class="breadcrumb">
        <a-breadcrumb>
          <a-breadcrumb-item>资源管理</a-breadcrumb-item>
          <a-breadcrumb-item>资源块管理</a-breadcrumb-item>
        </a-breadcrumb>
      </div>
      <a-space>
        <a-button type="primary" @click="onHandleCreate" v-auth="'Create'">
          <template #icon>
            <PlusOutlined />
          </template>
          添加资源块
        </a-button>
        <a-button type="primary" :loading="loading" @click="queryList">
          <template #icon>
            <SyncOutlined />
          </template>
          刷新
        </a-button>
        <!-- <a-button type="primary">
          <template #icon>
            <ExportOutlined />
          </template>
          导出
        </a-button> -->
      </a-space>
    </div>
    <div class="container">
      <div class="table">
        <a-table
          bordered
          :columns="columns"
          :row-key="(record) => record.id"
          :data-source="dataSource"
          :pagination="pagination"
          :loading="loading"
          @change="handleTableChange"
        >
        </a-table>
      </div>
    </div>
  </div>
  <EditView
    v-model:visible="popupModel.visible"
    :action="popupModel.action"
    :id="popupModel.id"
    v-if="popupModel.visible"
    @success="queryList"
  />
</template>

<script lang="ts" setup>
import {
  PlusOutlined,
  ExportOutlined,
  SyncOutlined,
  EditOutlined,
  DeleteOutlined,
} from "@ant-design/icons-vue";
import { useListPage } from "@/helpers/pages";
import api from "@/api/resource/bucket";
import { BucketDto } from "@/api/resource/types";
import { h, onMounted } from "vue";
import { PaginationConfig } from "ant-design-vue/es/pagination";
import { Button, ButtonGroup, message, Popconfirm } from "ant-design-vue";
import EditView from "./Edit.vue";
import { checkActionStyle } from "@/helpers";
const {
  loading,
  dataSource,
  columns,
  pagination,
  popupModel,
  createQueryModel,
  onHandleCreate,
  onHandleUpdate,
} = useListPage<BucketDto, string>({
  columns: [
    {
      dataIndex: "id",
      width: 200,
      title: "名称",
    },
    {
      dataIndex: "description",
      title: "描述",
    },
    {
      dataIndex: "status",
      width: 80,
      align: "center",
      title: "启用状态",
      customRender: ({ value }) => {
        return value ? "启用" : "停用";
      },
    },
    {
      dataIndex: "isPublic",
      width: 80,
      align: "center",
      title: "资源权限",
      customRender: ({ value }) => {
        return value ? "公开" : "私用";
      },
    },
    {
      dataIndex: "action",
      title: "操作",
      width: 150,
      customRender: ({ record }: any) => {
        return h(ButtonGroup, [
          h(
            Button,
            {
              type: "primary",
              size: "small",
              ghost: true,
              style: checkActionStyle("Update"),
              onClick() {
                onHandleUpdate(record);
              },
            },
            {
              icon() {
                return [h(EditOutlined)];
              },
              default() {
                return "修改";
              },
            }
          ),
          h(
            Popconfirm,
            {
              title: "删除提示",
              description: "确定要删除当前选择行数据信息吗？",
              onConfirm() {
                onDeleteItem(record);
              },
            },
            h(
              Button,
              {
                danger: true,
                size: "small",
                style: checkActionStyle("Delete"),
              },
              {
                icon() {
                  return [h(DeleteOutlined)];
                },
                default() {
                  return "删除";
                },
              }
            )
          ),
        ]);
      },
    },
  ],
});

const onDeleteItem = async (item: BucketDto) => {
  let callback = message.loading("数据操作中，请稍后");
  await api.delete(item.id);
  callback();
  await queryList();
};

const queryList = async () => {
  loading.value = true;
  let result = await api.getList(createQueryModel());
  dataSource.value = result.items;
  pagination.total = result.totalCount;
  loading.value = false;
};

const handleTableChange = (pageConfig: PaginationConfig, filters: any) => {
  pagination.current = pageConfig.current;
  queryList();
};

onMounted(() => {
  queryList();
});
</script>

<style lang="less" scoped></style>
